package myNewProject.repository;

import myNewProject.utils.CustomResult;
import myNewProject.utils.MySQLUtil;

import java.sql.*;
import java.util.List;

public class StoreByMySQL implements Repository {
    @Override
    public void outPut(List<CustomResult> parsedResult) {
        Connection connection = MySQLUtil.connect();
        System.out.println("正在储存数据至table：parsedResult");
        String query = "insert into parsedResult (title, url, posted_at, changed_at) values (?,?,?,?)";
        try(PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            for (CustomResult customResult : parsedResult) {
                preparedStatement.setString(1, customResult.getTitle());
                preparedStatement.setString(2, customResult.getUrl());
                preparedStatement.setDate(3, Date.valueOf(customResult.getCreatedAt()));
                preparedStatement.setTimestamp(4, Timestamp.valueOf(customResult.getUpdatedAt() + ":00"));
                preparedStatement.executeUpdate();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        MySQLUtil.close();
    }
}
